PagesScripts.forums = {};
PagesScripts.forums.items = {};

PagesScripts.forums.initHandlers = function() {
	PagesScripts.forums.items = Config.GetForumsList();
	if (PagesScripts.forums.items == null) {
		if (!HTTPEvents.hasEventListener("onForumList")) {
			HTTPEvents.addEventListener("onForumList", PagesScripts.forums.onForumList);
		}
	}
}

PagesScripts.forums.setCurrentForum = function(event) {
	Config.SetCurrentForum(event);
	PageManager.changePage("../forum_posts");
}

PagesScripts.forums.loadForums = function() {
	PagesScripts.forums.items = Config.GetForumsList();
	if (PagesScripts.forums.items == null) {
		HTTPManager.GetForumList();
	}
	else {
		PagesScripts.forums.populateForums(PagesScripts.forums.items);
	}
}

PagesScripts.forums.onForumList = function(event) {
	if (event.data && event.data.success != null && event.data.success === true && event.data.forums && event.data.forums.length > 0) {
		PagesScripts.forums.populateForums(event.data.forums);
		Config.SetForumsList(event.data.forums);
	}
}

PagesScripts.forums.populateForums = function(source) {
	for (var i=0;i<source.length;i++) {
		$("#forumItem").tmpl(source[i]).appendTo("#forum_list", $.mobile.activePage);
	}
	$("#forum_list", $.mobile.activePage).listview("refresh");
}